package com.ctms.system.controller;

import com.ctms.common.utils.MD5Utils;
import com.ctms.common.utils.PageUtils;
import com.ctms.common.utils.Query;
import com.ctms.common.utils.R;
import com.ctms.system.domain.*;
import com.ctms.system.service.ClassService;
import com.ctms.system.service.StudentService;
import com.ctms.system.service.TeacherService;
import com.ctms.system.service.TempStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

//上传的临时用户
@RequestMapping("/tempStudent")
@Controller
public class TempStudentController {

    @Autowired
    TempStudentService tempStudentService;
    @Autowired
    StudentService studentService;
    @Autowired
    ClassService classService;
    @Autowired
    TeacherService teacherService;

    @GetMapping("")
    public String temp(){
        return "sys/tempStudent/tempStudent";
    }

    @GetMapping("/list")
    @ResponseBody
    PageUtils list(@RequestParam Map<String,Object> param){
        Query query = new Query(param);
        List<TempStudentDO> tempStudentDOS = tempStudentService.selectAllTempStu(param);
        int total = tempStudentService.count(query);
        PageUtils pageUtils = new PageUtils(tempStudentDOS,total);
        return pageUtils;
    }

    @PostMapping("/save")
    @ResponseBody
    R save() {
        //先查询临时表的所有数据
        List<TempStudentDO> tempStudentDOS = tempStudentService.selectAllTemp();
        //判断是否有错误信息
        if (tempStudentService.CountByDescription()>0){
            return R.error();
        }else {
            //循环查询出的数据并一一插入
            for (int i=0;i<tempStudentDOS.size();i++){
                //获取此条数据对象
                TempStudentDO tempStudentDO = tempStudentDOS.get(i);
                UserDO userDO = new UserDO();
                //获取这个对象的班级名称
                String name = tempStudentDO.getStudentClass();
                //判断是否有此班级，等于1说明有此班级，0则没有
                int c = tempStudentService.countClass(name);
                //有此班级则直接插入用户表和中间表
                if (c == 1){
                    //值转换
                    userDO.setUsername(tempStudentDO.getUsername());
                    String username = userDO.getUsername();
                    String password = tempStudentDO.getPassword();
                    userDO.setPassword(MD5Utils.encrypt(username, password));

                    userDO.setName(tempStudentDO.getName());
                    userDO.setStudentClass(tempStudentDO.getStudentClass());
                    userDO.setStatus(1);
                    userDO.setIdentity(2);
                    userDO.setGmtModified(tempStudentDO.getGmtModified());
                    userDO.setGmtCreate(tempStudentDO.getGmtCreate());
                    userDO.setSex(tempStudentDO.getSex());
                    userDO.setEmail(tempStudentDO.getEmail());
                    userDO.setMobile(tempStudentDO.getMobile());
                    //插入sys_user用户表
                    if (studentService.insertStudent(userDO)>0){
                        //插入成功后获取此条数据的user_id
                        int user_id = studentService.selectMaxIdByTime();
                        //插入用户角色表
                        int role_id = 3;
                        UserRoleDO userRoleDO=new UserRoleDO();
                        userRoleDO.setUserId(Long.parseLong(Integer.toString(user_id)));
                        userRoleDO.setRoleId(Long.parseLong(Integer.toString(role_id)));
                        teacherService.insertUserRole(userRoleDO);

                        int class_id = studentService.getClassIdByName(name);
                        //插入学生班级中间表
                        ClassStudentDO classStudentDO = new ClassStudentDO();
                        classStudentDO.setUserId(user_id);
                        classStudentDO.setClassId(class_id);
                        studentService.insertClassStudent(classStudentDO);
                        tempStudentService.deleteTempStu(tempStudentDO.getUid());
                        return R.ok();
                    }else {
                        return R.error();
                    }
                }else {
                    //值转换
                    userDO.setUsername(tempStudentDO.getUsername());
                    String username = userDO.getUsername();
                    String password = tempStudentDO.getPassword();
                    userDO.setPassword(MD5Utils.encrypt(username, password));

                    userDO.setName(tempStudentDO.getName());
                    userDO.setStudentClass(tempStudentDO.getStudentClass());
                    userDO.setStatus(1);
                    userDO.setIdentity(2);
                    userDO.setGmtModified(tempStudentDO.getGmtModified());
                    userDO.setGmtCreate(tempStudentDO.getGmtCreate());
                    userDO.setSex(tempStudentDO.getSex());
                    userDO.setEmail(tempStudentDO.getEmail());
                    userDO.setMobile(tempStudentDO.getMobile());
                    if (studentService.insertStudent(userDO)>0){
                        //插入成功后获取此条数据的user_id
                        int user_id = studentService.selectMaxIdByTime();
                        //插入用户角色表
                        int role_id = 3;
                        UserRoleDO userRoleDO=new UserRoleDO();
                        userRoleDO.setUserId(Long.parseLong(Integer.toString(user_id)));
                        userRoleDO.setRoleId(Long.parseLong(Integer.toString(role_id)));
                        teacherService.insertUserRole(userRoleDO);
                        //没有此班级，需要先创建班级
                        ClassDO classDO = new ClassDO();
                        //设置班级名称
                        classDO.setClassName(name);
                        if (classService.insertClass(classDO)>0){
                            //查找最大班级id
                            int class_id = tempStudentService.selectMaxId();
                            ClassStudentDO classStudentDO = new ClassStudentDO();
                            classStudentDO.setClassId(class_id);
                            classStudentDO.setUserId(user_id);
                            studentService.insertClassStudent(classStudentDO);
                            tempStudentService.deleteTempStu(tempStudentDO.getUid());
                        }
                        return R.ok();
                    }else {
                        return R.error();
                    }
                }
            }
            return R.ok();
        }
    }

    @RequestMapping(value = "/ajaxUpload",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public String ajaxUploadExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String result = tempStudentService.ajaxUploadExcel(request, response);
        return result;
    }

    @PostMapping("/remove")
    @ResponseBody
    R deleteTempStu(Integer uid){
        if (tempStudentService.deleteTempStu(uid)>0){
            return R.ok();
        }else {
            return R.error();
        }
    }
}
